<?php
/**
 * Created by PhpStorm.
 * User: Liulibao
 * Date: 2019/10/20
 * Time: 10:18
 */

namespace app\common\model;


use think\Model;

class AfficheReadModel extends Model
{
    protected $table = 'ds_affiche_reads';

    public function getListByMemberId($member_id)
    {
        if ($member_id <=0 ) return [];
        return db('affiche_reads')->where('member_id', $member_id)->field('affiche_id')->select();
    }

    /**
     * @description:通过member_id 获取阅读公告数量  @MrLiu
     * @param $member_id
     * @return bool|int|string
     */
    public function getCountByMemberId($member_id)
    {
        if ($member_id <=0 ) return [];
        return $this->where('member_id', $member_id)->count('id');
    }

    /**
     * @description: 添加数据 @MrLiu
     * @param $where
     * @param $data
     */
    public function createData($where, $data)
    {
        if (!$this->getOneByCondition($where['member_id'], $where['affiche_id'])) {
            $this->insert($data);
        }
    }

    /**
     * @description: 根据条件获取数据  @MrLiu
     * @param $member_id
     * @param $affiche_id
     * @return false|mixed|\PDOStatement|string|\think\Collection
     */
    public function getOneByCondition($member_id, $affiche_id)
    {
        $cacheData = cache('getAfficheReadUserId_'.$member_id.'_'.$affiche_id);
        if (empty($cacheData)) {
            $where = array('member_id' => $member_id, 'affiche_id' => $affiche_id);
            $result = $this->where($where)->select();
            if (!empty($result)) {
                cache('getAfficheReadUserId_'.$member_id.'_'.$affiche_id, $result);
            }
            return $result;
        } else {
            return $cacheData;
        }
    }
}